#pragma once
#include "../stdafx.h"
#include "afxmt.h"

template<typename T>
class CQueue: public CTypedPtrList<CPtrList, T>
{
private:
	CSemaphore* m_smpLockObj;

public:

	CQueue(void)
	{
		m_smpLockObj = new CSemaphore(0, 0x7FFFFFFF);
		m_smpLockObj->Unlock();
	}

	void Push(T msg)
	{
		m_smpLockObj->Lock();
		AddTail(msg);
		m_smpLockObj->Unlock();
	}

	T Pop()
	{
		T ret;
		m_smpLockObj->Lock();
		ret = IsEmpty() ? NULL : RemoveHead();
		m_smpLockObj->Unlock();
		return ret;
	}

	int Count()
	{
		int ret;
		m_smpLockObj->Lock();
		ret = this->GetCount();
		m_smpLockObj->Unlock();	
		return ret;
	}

	void Clear()
	{
		m_smpLockObj->Lock();
		this->RemoveAll();
		m_smpLockObj->Unlock();
	}

	~CQueue(void)
	{
		m_smpLockObj->Unlock();
		delete m_smpLockObj;
	}
};
